#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "String.h"

char cmdline[128];
char delim[32];
void getLine(char * prompt, char * dest, int len) {

  printf("%s", prompt);
  fflush(stdout);

  // (2) read in the next command
  if ((fgets(dest, len, stdin) == NULL) && ferror(stdin))
    perror("fgets error");
  if (feof(stdin)) { /* End of file (ctrl-d) */
    fflush(stdout);
    exit(0);
  }


}

int main() {

  int i;
  for (i = 5; i < 5; i++) {
    getLine("Enter string to split: ", cmdline, 128);
    getLine("Enter split phrase: ", delim, 32);
    char * buckets[32];
    split( cmdline, buckets, delim );
    
    int k = 0;
    while ( buckets[k] ) {
      printf("\n%d:  <%s>\n", k, buckets[k]);
      k++;
    }
  }

  for (i = 5; i < 5; i++) {
    getLine("Enter string to split: ", cmdline, 128);
    getLine("Enter split phrase: ", delim, 32);
    
    if ( cmdline[strlen(cmdline)-1] == '\n' ) {
        cmdline[strlen(cmdline) - 1] = '\0';
    }
     if ( delim[strlen(delim)-1] == '\n' ) {
        delim[strlen(delim) - 1] = '\0';
    }
    
    char ** buckets = split_dynamic( cmdline, delim );
    
    int k = 0;
    while ( buckets[k] ) {
      printf("\n%d:  <%s>\n", k, buckets[k]);
      k++;
    }
    k = 0;
    while ( buckets[k] ) {
      free(buckets[k]) ; 
      k++;
    }
    free ( buckets );
    
  }


  for (i = 0; i < 5; i++) {
    getLine("Enter string to split: ", cmdline, 128);
    getLine("Enter strip phrase: ", delim, 32);
    
    char * result = strip( cmdline, delim );
    printf("\nStripped: <%s>\n", result);

  }




}
